package weibo.network;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import inter.Delegate;
import managers.VolleySingleton;
import weibo.dto.Blog;

/**
 * 获取微博内容
 * Created by Administrator on 2016/4/1.
 */
public class GetWeibo {
    Delegate delegate;
    StringRequest stringRequest;

    RequestBlog requestBlog;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setRequestBlog(RequestBlog requestBlog) {
        this.requestBlog = requestBlog;
    }

    public void get() {
        String ip = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + ip + "/Blog/GetNewBlog2";
        stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("获取新闻", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data",gson.toJson(requestBlog));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }


    public static class RequestBlog {
        private Long blog_ID;
        private String isNew;
        private String className;

        public Long getBlog_ID() {
            return blog_ID;
        }
        public void setBlog_ID(Long blog_ID) {
            this.blog_ID = blog_ID;
        }
        public String getIsNew() {
            return isNew;
        }
        public void setIsNew(String isNew) {
            this.isNew = isNew;
        }

        public String getClassName() {
            return className;
        }

        public void setClassName(String className) {
            this.className = className;
        }
    }

    public static class ResponseGetBlog {
        private String status;
        private List<Blog> blogList;

        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public List<Blog> getBlogList() {
            return blogList;
        }
        public void setBlogList(List<Blog> blogList) {
            this.blogList = blogList;
        }
    }

}
